package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传的controller
 */
@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        // 接收文件
        // log.info("接收到的参数是：username={},age={},image={}",username,age,image.getOriginalFilename());
        // String s = new String(image.getBytes(), StandardCharsets.UTF_8);
        // log.info("文件的内容：{}", s);
        // 通过输入流获取上传的文件内容
        // InputStream inputStream = image.getInputStream();
        String url = aliyunOssUtil.upload(image);
        log.info("上传之后的文件url：{}", url);
        return Result.success(url);
    }
}
